package com.example.demo.server.controller;

import com.example.demo.ds.MyHashMap;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.*;
import com.example.demo.server.model.ArticleInfo;
import com.example.demo.server.model.UserInfo;
import com.example.demo.server.service.ArticleService;
import com.example.demo.server.service.UserService;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class ArticleController {

    private ArticleService articleService = new ArticleService();
    private UserService userService = new UserService();

    public CommonResponse publish(ArticleInfo articleInfo) {
        int rows = articleService.publish(articleInfo);
        return CommonResponse.success(rows);
    }

    public CommonResponse getMyArts(UserInfo userInfo) {
        MyList<ArticleInfo> myList = articleService.getMyArts(userInfo.getId());
        ArticleUtils.substringList(myList);
        return CommonResponse.success(myList);
    }


    public CommonResponse delete(ArticleInfo articleInfo) {
        int rows = articleService.delete(articleInfo);
        return CommonResponse.success(rows);
    }

    public CommonResponse update(ArticleInfo articleInfo) {
        int rows = articleService.update(articleInfo);
        return CommonResponse.success(rows);
    }

    public CommonResponse detail(ArticleInfo articleInfo) {
        // 3. 根据uid查询用户总文章数的任务
        FutureTask<Integer> task1 = new FutureTask<Integer>(() -> {
            return articleService.getArtNumberByUid(articleInfo.getUid());
        });
        // 4. 根据uid查询用户信息的任务
        FutureTask<UserInfo> task2 = new FutureTask<UserInfo>(() -> {
            return userService.getUserByUid(articleInfo.getUid());
        });
        // 5. 根据aid更新阅读量的任务
        FutureTask<Integer> task3 = new FutureTask<Integer>(() -> {
            return articleService.incrementRCount(articleInfo.getId());
        });
        // 6. 线程池执行任务
        APPVariable.THREAD_POOL.submit(task1);
        APPVariable.THREAD_POOL.submit(task2);
        APPVariable.THREAD_POOL.submit(task3);
        // 7. 构造响应数据,并返回
        MyHashMap<String, Object> map = new MyHashMap<>();
        try {
            map.put("count", task1.get());
            map.put("user", task2.get());
            map.put("art", articleInfo);
            task3.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return CommonResponse.success(map);
    }

    public CommonResponse allList(Integer pindex, Integer psize)  {

        Integer index = pindex;
        Integer size = psize;
        FutureTask<MyList<ArticleInfo>> task1 = new FutureTask<MyList<ArticleInfo>>(() -> {
            int offset = size * (index - 1);
            return articleService.getSubList(size, offset);
        });
        FutureTask<Integer> task2 = new FutureTask<Integer>(() -> {
            return articleService.getNumber();
        });
        APPVariable.THREAD_POOL.submit(task1);
        APPVariable.THREAD_POOL.submit(task2);
        //因为数据库的一条语句是保证原子性的，所以线程安全
        // 3. 数据进一步处理
        int count = 0;
        MyList<ArticleInfo> list = null;
        int pMax = 0;
        try {
            count = task2.get();
            pMax = (int)Math.ceil(count * 1.0 / size);
            list = task1.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        ArticleUtils.substringList(list);
        // 4. 构造数据并返回
        MyHashMap<String, Object> map = new MyHashMap<>();
        map.put("list", list);
        map.put("pMax", pMax);
        return CommonResponse.success(map);
    }


}
